package com.dragon.mmochat.model.role;

import com.dragon.mmochat.model.MmochatSerial;
import com.dragon.mmochat.model.enumType.MmochatNpcFaceType;
import com.dragon.mmochat.model.enumType.MmochatNpcState;
import com.dragon.mmochat.service.MmochatDao;
import com.dragon.mmochat.util.MmochatUtil;
import com.google.gson.reflect.TypeToken;

//临时NPC
public class MmochatTempNpc extends MmochatNpc {
	private static final long serialVersionUID = -7394686785969582899L;
	protected static MmochatSerial serial = new MmochatSerial();
	protected static int lastSavedId = -1;
	protected MmochatNpcState state = MmochatNpcState.空闲; // 状态
	protected long lifeTime; // 生命时间, ms
	protected long createTime = System.currentTimeMillis(); // 出生时间
	protected int line; // 线
	private int level = 1; // 等级
	private int minTeamNum = 1;

	public MmochatTempNpc() {
		// TODO:空构造仅限Gson使用
	}

	public MmochatTempNpc(boolean init) {
		if (init) {
			int bossId = -1 * serial.addAndGetSerialId();
			if (-1 * bossId == Integer.MAX_VALUE) {
				serial.setUniqueId(1);
				bossId = -1;
				MmochatDao.decreaseTempTaskSerial(lastSavedId - bossId);
				lastSavedId = bossId;
			}
			setNpcId(bossId);
			if (lastSavedId - bossId >= 1000) {
				MmochatDao.decreaseBossSerial(lastSavedId - bossId);
				lastSavedId = bossId;
			}
		}
		if (MmochatUtil.isInPercent(50)) {
			face = MmochatNpcFaceType.朝左;
		} else {
			face = MmochatNpcFaceType.朝右;
		}
	}

	public MmochatTempNpc getClone() {
		return (MmochatTempNpc) MmochatUtil.deepClone(this);
	}

	public static void initSerial(int initValue) {
		serial.setUniqueId(-1 * initValue);
		lastSavedId = initValue;
	}

	public static int getCurBossId() {
		return -1 * serial.getSerialId();
	}

	public static int getNewNpcId() {
		int bossId = serial.addAndGetSerialId();
		bossId = -1 * bossId;
		if (lastSavedId - bossId >= 1000) {
			MmochatDao.decreaseBossSerial(lastSavedId - bossId);
			lastSavedId = bossId;
		}
		return bossId;
	}

	public MmochatNpcState getState() {
		return state;
	}

	public void setState(MmochatNpcState state) {
		this.state = state;
	}

	public long getLifeTime() {
		return lifeTime;
	}

	public void setLifeTime(long lifeTime) {
		this.lifeTime = lifeTime;
	}
	
	public boolean isTimeOut(){
		return System.currentTimeMillis() - createTime >= lifeTime;
	}

	public long getCreateTime() {
		return createTime;
	}

	public void setCreateTime(long createTime) {
		this.createTime = createTime;
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getMinTeamNum() {
		return minTeamNum;
	}

	public void setMinTeamNum(int minTeamNum) {
		this.minTeamNum = minTeamNum;
	}

}
